
package dialogs;

import core.COINCOMOComponent;
import core.COINCOMOSubComponent;
import core.COINCOMOSubSystem;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.xml.ws.Response;
import main.GlobalMethods;
import main.Icons;
import panels.ComponentOverviewPanel;
import database.COINCOMOComponentManager;
import database.COINCOMOSubSystemManager;


/**
 *
 * @author Raed Shomali
 */
public class CotGlueCodeEAFTabDialog extends JDialog implements ActionListener, ChangeListener
{


    char popupflag='n';
    // Function Points

    

    // Tabs and Buttons
    JTabbedPane tabs = new JTabbedPane(JTabbedPane.TOP,JTabbedPane.SCROLL_TAB_LAYOUT);
    JButton applyButton = new JButton( "Apply" );
    JButton closeButton = new JButton( "Close" );
    COINCOMOComponent component = null;

    int rowNumber = 0;

    public CotGlueCodeEAFTabDialog( JFrame owner , COINCOMOComponent component , int rowNumber )
    {
        super( owner );

        this.setModal( true );
        this.setTitle( "Size - " + component.getName() );
        this.rowNumber = rowNumber;
        this.component = component;
        GlobalMethods.updateStatusBar( "Done." );
  

        // Buttons
        applyButton.addActionListener( this );
        closeButton.addActionListener( this );

        applyButton.setFocusable( false );
        closeButton.setFocusable( false );

        applyButton.setIcon( Icons.SAVE_ICON );
        closeButton.setIcon( Icons.CLOSE_ICON );

        // Panel Tabs
      
        
        // Loading ...
        //loadParameters( owner );



        // Tabs
        
        ArrayList<String> cotsClasses = component.getCotClass();
        ArrayList<Integer> cotsClassesID = component.getCotClassID();
        //JPanel j = new JPanel( new CotGlueCodeEAFDialog(owner, subSystem, rowNumber));
        for(int i = 0; i < cotsClasses.size(); i++)
        {
        	
        	tabs.addTab( cotsClasses.get(i) , new CotGlueCodeEAFPanel(owner, component, cotsClassesID.get(i) ));
        	if (i > 0)
        		tabs.setBackgroundAt(i, Color.red);
        	else
        		tabs.setBackgroundAt(i, Color.green);
        }
        tabs.addChangeListener( this ) ;

        // GUI
        JPanel northPanel = new JPanel( null );
        northPanel.setPreferredSize( new Dimension( 100 , 120 ) );
        JPanel southPanel = new JPanel();
        JPanel checkBoxPanel = new JPanel();
       

        southPanel.add( applyButton );
        southPanel.add( closeButton );

        this.setLayout( new BorderLayout() );
        this.add(checkBoxPanel, BorderLayout.PAGE_START);
        this.add( northPanel , BorderLayout.NORTH );
        this.add( tabs );
        this.add( southPanel , BorderLayout.SOUTH );

        

        this.setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
        this.setLocation( this.getOwner().getX() + 100 , this.getOwner().getY() + 100 );
        this.setResizable( true );
        this.setSize(640 , 625 );
        this.setVisible( true );
    }

    /**
     *
     */
    public void loadParameters( JFrame owner )
    {
    	Set<Integer> loadedCotIds = component.getLoadedCocotsRatings().keySet();
    	
    	ArrayList<String> cotsClasses = new ArrayList<String>();
    	ArrayList<Integer> cotsClassesID = new ArrayList<Integer>();
    	for(Integer load : loadedCotIds)
    	{
    		cotsClasses.add( GlobalMethods.convertCotIdToString(load.intValue()) );
    		cotsClassesID.add( load );
    	}
        
        //JPanel j = new JPanel( new CotGlueCodeEAFDialog(owner, subSystem, rowNumber));
        for(int i = 0; i < cotsClasses.size(); i++)
        {
        	
        	tabs.addTab( cotsClasses.get(i) , new CotGlueCodeEAFPanel(owner, component, cotsClassesID.get(i) ));
        	if (i > 0)
        		tabs.setBackgroundAt(i, Color.red);
        	else
        		tabs.setBackgroundAt(i, Color.green);
        }
        tabs.addChangeListener( this ) ;
     
        applyButton.setEnabled( true );
        closeButton.setEnabled( true );

        GlobalMethods.updateStatusBar( "Parameters Loaded." );
       
    }

    public void actionPerformed( ActionEvent e )
    {
        if ( e.getSource() == applyButton )
        {
        	boolean tabsColors = true;
        	ArrayList<Integer> cotsId = component.getCotClassID();
        	Set<Integer> loadedIds = component.getLoadedCocotsRatings().keySet();
    		for(Integer id : loadedIds)
    			cotsId.add(id);
        	for(int i = 0; i < tabs.getTabCount(); i++)
        	{
        		if(tabs.getBackgroundAt(i).equals(Color.red))
        			tabsColors = false;
        	}
        	if(!tabsColors)
        	{
        		int n = JOptionPane.showConfirmDialog(this, "Some tabs have not been visited, do you still want to continue?", "Warning",JOptionPane.YES_NO_OPTION);
        		
        		if(n > 0)
        			return;
        	}
        	for(int i = 0; i < tabs.getTabCount(); i++)
        	{
        		((CotGlueCodeEAFPanel)tabs.getComponentAt(i)).getRatings(cotsId.get(i));
        		
        	}
        	SwingUtilities.invokeLater( new Runnable()
            {
                public void run()
                {
                 	//COINCOMOSubSystemManager.insertCocotCost( subSystem );
                	COINCOMOComponentManager.insertGlueCost(component);
                }
            });
        	this.dispose();
        }
       if ( e.getSource() == closeButton )
        {
            // Free Resources ... Close Window
            this.dispose();
        }
         
        
    }

    public void stateChanged( ChangeEvent e )
    {
        JTabbedPane pane = (JTabbedPane)e.getSource();

        //component.setSizingType( pane.getSelectedIndex() );
        
        pane.setBackgroundAt(pane.getSelectedIndex(), Color.green);
    }
}
